package org.dragonnova.meetingapp.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import org.dragonnova.meetingapp.model.bean.MeetingInfo;
import org.dragonnova.meetingapp.model.bean.VoiceInfo;

import java.util.ArrayList;

/**
 * Created by hucn on 2016/1/10.
 * Description:一个用于封装contentresolver查询方法的工具类
 */
public class ContentResolverUtil {
    private Context mContext;
    private ContentResolver mResolver;

    public ContentResolverUtil(Context context) {
        this.mContext = context;
        mResolver = mContext.getContentResolver();
    }

    /**
     * 保存聊天记录
     */
    public int insertChatHistory(VoiceInfo bean) {
        /*String orgContent, String tranContent,long inserttime, char type,
                        String orgPath,int orgCosttime, String userId,int flag*/
        ContentValues cv = new ContentValues();
        cv.put(VoiceInfo.KEY_ORG_TRAN_TEXT_COLUMN, bean.getOrgContent());
        cv.put(VoiceInfo.KEY_TRAN_TEXT_COLUMN, bean.getTranContent());
        cv.put(VoiceInfo.KEY_INSERT_DATE_COLUMN, bean.getInsertTime());
        cv.put(VoiceInfo.KEY_VOICE_TYPE_COLUMN, String.valueOf(bean.getType()));
        cv.put(VoiceInfo.KEY_ORG_PATH_COLUMN, bean.getOrgPath());
        cv.put(VoiceInfo.KEY_VOICE_ORG_COST_TIME_COLUMN, bean.getOrgCostTime());
        cv.put(VoiceInfo.KEY_VOICE_USER_ID_FOREIGN_COLUMN, bean.getUserId());
        cv.put(VoiceInfo.KEY_VOICE_FLAG_COLUMN, bean.getFlag());
        Uri resultUri = mResolver.insert(VoiceInfo.CONTENT_VOICE_URI, cv);
        return getInsertResultInUri(resultUri);
    }

    /**
     * 保存聊天记录
     */
    public int insertChatHistory(VoiceInfo bean, MeetingInfo info) {
        /*String orgContent, String tranContent,long inserttime, char type,
                        String orgPath,int orgCosttime, String userId,int flag*/
        ContentValues cv = new ContentValues();
        cv.put(VoiceInfo.KEY_ORG_TRAN_TEXT_COLUMN, bean.getOrgContent());
        cv.put(VoiceInfo.KEY_TRAN_TEXT_COLUMN, bean.getTranContent());
        cv.put(VoiceInfo.KEY_INSERT_DATE_COLUMN, bean.getInsertTime());
        cv.put(VoiceInfo.KEY_VOICE_TYPE_COLUMN, String.valueOf(bean.getType()));
        cv.put(VoiceInfo.KEY_ORG_PATH_COLUMN, bean.getOrgPath());
        cv.put(VoiceInfo.KEY_VOICE_ORG_COST_TIME_COLUMN, bean.getOrgCostTime());
        cv.put(VoiceInfo.KEY_VOICE_USER_ID_FOREIGN_COLUMN, bean.getUserId());
        cv.put(VoiceInfo.KEY_VOICE_FLAG_COLUMN, bean.getFlag());
        cv.put(VoiceInfo.KEY_MEETING_ID_COLUMN, info.getSerial());
        Uri resultUri = mResolver.insert(VoiceInfo.CONTENT_VOICE_URI, cv);
        return getInsertResultInUri(resultUri);
    }

    /**
     * 查询聊天记录
     */
    public ArrayList<VoiceInfo> findAllHistory() {
        Uri ContentUris = VoiceInfo.CONTENT_VOICE_URI;
        Uri uri = Uri.withAppendedPath(ContentUris, "");
        Cursor cursor = mResolver.query(uri, null, null, null, null);
        ArrayList<VoiceInfo> listinfos = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                long id = cursor.getLong(cursor.getColumnIndex(VoiceInfo.KEY_ID_COLUMN));
                String orgContent = cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_ORG_TRAN_TEXT_COLUMN));
                String tranContent = cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_TRAN_TEXT_COLUMN));
                long inserttime = cursor.getLong(cursor.getColumnIndex(VoiceInfo.KEY_INSERT_DATE_COLUMN));
                char type = (cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_VOICE_TYPE_COLUMN))).charAt(0);
                String orgPath = cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_ORG_PATH_COLUMN));
                int orgCosttime = cursor.getInt(cursor.getColumnIndex(VoiceInfo.KEY_VOICE_ORG_COST_TIME_COLUMN));
                String userId = cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_VOICE_USER_ID_FOREIGN_COLUMN));
                int flag = cursor.getInt(cursor.getColumnIndex(VoiceInfo.KEY_VOICE_FLAG_COLUMN));
                long meetingId = cursor.getLong(cursor.getColumnIndex(VoiceInfo.KEY_MEETING_ID_COLUMN));
                VoiceInfo voiceInfo = new VoiceInfo(id, orgContent, tranContent, inserttime, type, orgPath, orgCosttime, userId, flag, meetingId);
                listinfos.add(voiceInfo);
            }
            cursor.close();
            return listinfos;
        }
        return null;
    }

    /**
     * 删除聊天记录
     */
    public int deleteAllChatHistory(String userId) {
        return mResolver.delete(VoiceInfo.CONTENT_VOICE_URI, "USER_ID=?", new String[]{userId});
    }

    /**
     * 根据id删除聊天记录
     */
    public int deleteChatHistoryById(String userId, long id) {
        return mResolver.delete(VoiceInfo.CONTENT_VOICE_URI, "_id=? and USER_ID=?", new String[]{String.valueOf(id), userId});
    }

    /**
     * 插入meeting记录
     */
    public int insertMeeting(MeetingInfo bean) {
        ContentValues cv = new ContentValues();
        cv.put(MeetingInfo.KEY_SERIAL_COLUMN, bean.getSerial());
        cv.put(MeetingInfo.KEY_START_TIME_COLUMN, bean.getStartTime());
        cv.put(MeetingInfo.KEY_END_TIME_COLUMN, bean.getEndTime());
        cv.put(MeetingInfo.KEY_DURATION_COLUMN, bean.getDuration());
        cv.put(MeetingInfo.KEY_NAME_COLUMN, bean.getName());
        cv.put(MeetingInfo.KEY_PASSWORD_COLUMN, bean.getPassword());
        cv.put(MeetingInfo.KEY_FLAG_COLUMN, bean.getFlag());
        Uri resultUri = mResolver.insert(MeetingInfo.CONTENT_MEETING_URI, cv);
        return getInsertResultInUri(resultUri);
    }

    public int updateMeeting(MeetingInfo bean) {
        ContentValues cv = new ContentValues();
        cv.put(MeetingInfo.KEY_NAME_COLUMN, bean.getName());
        cv.put(MeetingInfo.KEY_PASSWORD_COLUMN, bean.getPassword());
        return mResolver.update(MeetingInfo.CONTENT_MEETING_URI, cv, "serial=?", new String[]{String.valueOf(bean.getSerial())});
    }

    public int collectMeeting(MeetingInfo bean) {
        ContentValues cv = new ContentValues();
        cv.put(MeetingInfo.KEY_FLAG_COLUMN, 2);
        return mResolver.update(MeetingInfo.CONTENT_MEETING_URI, cv, "serial=?", new String[]{String.valueOf(bean.getSerial())});
    }

    /**
     * 查询会议记录
     */
    public ArrayList<MeetingInfo> findAllMeeting() {
        Uri ContentUris = MeetingInfo.CONTENT_MEETING_URI;
        Uri uri = Uri.withAppendedPath(ContentUris, "");
        Cursor cursor = mResolver.query(uri, null, null, null, null);
        ArrayList<MeetingInfo> listinfos = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                long id = cursor.getLong(cursor.getColumnIndex(MeetingInfo.KEY_ID_COLUMN));
                long serial = cursor.getLong(cursor.getColumnIndex(MeetingInfo.KEY_SERIAL_COLUMN));
                long startTime = cursor.getLong(cursor.getColumnIndex(MeetingInfo.KEY_START_TIME_COLUMN));
                long endTime = cursor.getLong(cursor.getColumnIndex(MeetingInfo.KEY_END_TIME_COLUMN));
                long duration = cursor.getLong(cursor.getColumnIndex(MeetingInfo.KEY_DURATION_COLUMN));
                String name = cursor.getString(cursor.getColumnIndex(MeetingInfo.KEY_NAME_COLUMN));
                String password = cursor.getString(cursor.getColumnIndex(MeetingInfo.KEY_PASSWORD_COLUMN));
                int flag = cursor.getInt(cursor.getColumnIndex(MeetingInfo.KEY_FLAG_COLUMN));
                MeetingInfo meetingInfo = new MeetingInfo(id, serial, startTime, endTime, duration, name, password, flag);
                listinfos.add(meetingInfo);
            }
            cursor.close();
            return listinfos;
        }
        return null;
    }

    /**
     * 根据会议序列号查找会议id
     */
    public int findMeetingBySerial(long serial) {
        Cursor cursor = mResolver.query(MeetingInfo.CONTENT_MEETING_URI, null, "serial=?", new String[]{String.valueOf(serial)}, null);
        if (cursor != null) {
            if (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex(MeetingInfo.KEY_ID_COLUMN));
                cursor.close();
                return id;
            }
            cursor.close();
        }
        return -1;
    }

    /**
     * 根据meeting serial 查询聊天记录
     */
    public ArrayList<VoiceInfo> findHistoryById(String meetingid) {
        Uri ContentUris = Uri.parse(VoiceInfo.CONTENT_VOICE_URI.toString() + "/" + "meeting");
        Uri uri = Uri.withAppendedPath(ContentUris, "");
        Cursor cursor = mResolver.query(uri, null, "MEETING_ID=?", new String[]{meetingid}, null);
        ArrayList<VoiceInfo> listinfos = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                long id = cursor.getLong(cursor.getColumnIndex(VoiceInfo.KEY_ID_COLUMN));
                String orgContent = cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_ORG_TRAN_TEXT_COLUMN));
                String tranContent = cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_TRAN_TEXT_COLUMN));
                long inserttime = cursor.getLong(cursor.getColumnIndex(VoiceInfo.KEY_INSERT_DATE_COLUMN));
                char type = (cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_VOICE_TYPE_COLUMN))).charAt(0);
                String orgPath = cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_ORG_PATH_COLUMN));
                int orgCosttime = cursor.getInt(cursor.getColumnIndex(VoiceInfo.KEY_VOICE_ORG_COST_TIME_COLUMN));
                String userId = cursor.getString(cursor.getColumnIndex(VoiceInfo.KEY_VOICE_USER_ID_FOREIGN_COLUMN));
                int flag = cursor.getInt(cursor.getColumnIndex(VoiceInfo.KEY_VOICE_FLAG_COLUMN));
                long meetingId = cursor.getLong(cursor.getColumnIndex(VoiceInfo.KEY_MEETING_ID_COLUMN));
                VoiceInfo voiceInfo = new VoiceInfo(id, orgContent, tranContent, inserttime, type, orgPath, orgCosttime, userId, flag, meetingId);
                listinfos.add(voiceInfo);
            }
            cursor.close();
            return listinfos;
        }
        return null;
    }

    /**
     * 获取insert返回uri的最后拼接的insert返回值
     */
    private int getInsertResultInUri(Uri uri) {
        String uriStr = uri.toString();
        int lastIndexOf = uriStr.lastIndexOf("/");
        String insertResult = uriStr.substring(lastIndexOf + 1);
        try {
            return Integer.valueOf(insertResult);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return -1;
        }
    }
}
